package com.example.flower_shop.controller.admin;

import com.example.flower_shop.common.ApiResponse;
import com.example.flower_shop.dto.review.ReviewDTO;
import com.example.flower_shop.service.ReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Slf4j
@Api(tags = "商家评论管理")
@RestController
@RequestMapping("/api/admin/reviews")
@PreAuthorize("hasRole('MERCHANT')")
@RequiredArgsConstructor
public class ReviewManageController {

    private final ReviewService reviewService;

    @ApiOperation("获取所有评论列表")
    @GetMapping
    public ApiResponse<Page<ReviewDTO>> getAllReviews(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String date,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        log.info("商家获取评论列表, keyword: {}, status: {}, date: {}, page: {}, size: {}",
                keyword, status, date, page, size);

        try {
            Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createTime"));

            // 使用参数调用服务方法
            Page<ReviewDTO> reviews = reviewService.getAllReviews(keyword, status, date, pageable);

            log.info("获取评论成功, 共{}条评论", reviews.getTotalElements());
            return ApiResponse.success(reviews);
        } catch (Exception e) {
            log.error("获取评论失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }

    @ApiOperation("删除违规评论")
    @DeleteMapping("/{reviewId}")
    public ApiResponse<Void> deleteViolationReview(@PathVariable Long reviewId) {
        log.info("商家删除违规评论, reviewId: {}", reviewId);

        try {
            // 调用特殊的删除方法，表示这是商家删除违规评论
            reviewService.deleteMerchantReview(reviewId);
            log.info("删除违规评论成功, reviewId: {}", reviewId);
            return ApiResponse.success(null);
        } catch (Exception e) {
            log.error("删除违规评论失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }

    @ApiOperation("更新评论状态")
    @PutMapping("/{reviewId}/status")
    public ApiResponse<Void> updateReviewStatus(
            @PathVariable Long reviewId,
            @RequestParam Integer status) {
        log.info("商家更新评论状态, reviewId: {}, status: {}", reviewId, status);

        try {
            reviewService.updateReviewStatus(reviewId, status);
            log.info("更新评论状态成功, reviewId: {}", reviewId);
            return ApiResponse.success(null);
        } catch (Exception e) {
            log.error("更新评论状态失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
}